package shun.demo.curve;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import shun.demo.utils.DeviceUtil;

/**
 * Created by chen on 3/21/14.
 */
public class SineView extends View {

    private SineCurve mSineCurve;
    private Paint mPaint;
    private Paint mLinePaint;
    public SineView(Context context) {
        super(context);
        init(context);
    }

    public SineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(0xffffffff);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mLinePaint = new Paint(mPaint);
        mLinePaint.setColor(0xff55ffff);
        mLinePaint.setStyle(Paint.Style.FILL_AND_STROKE);
//        mPaint.setPathEffect(new DashPathEffect(new float[] {10, 5, 5, 5}, 0));
        mSineCurve = new SineCurve();
        mSineCurve.sample(DeviceUtil.getDeviceWidth());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Path path = mSineCurve.asPath();
        Path path1 = new Path(path);
        Path path2 = new Path();
        float[] points = mSineCurve.asPoints();
        path2.lineTo(getWidth() / 3, 0);
        path2.lineTo(getWidth() / 3, points[2 * getWidth() / 3 + 1]);
        path1.moveTo(0, 0);
        path1.addPath(path2);

        canvas.translate(0, getHeight() / 2);
//        canvas.drawLines(points, mPaint);
//        canvas.drawLine(0, 0, getWidth(), 0, mPaint);
        canvas.drawPath(path1, mPaint);

        Path path3 = new Path();
        path3.lineTo(getWidth(), 0);

        Path path4 = new Path();
        path4.lineTo(getWidth() / 3, getHeight() / 3);

        path3.moveTo(getWidth() / 2, 0);
        path3.addPath(path4);

        path3.lineTo(getWidth() / 3, 0);

//        path3.close();

        canvas.translate(0, 10);
        canvas.drawPath(path3, mLinePaint);

    }
}
